#!/bin/env bash
black="\e[30m"
red="\033[31m"
green="\033[32m"
yellow="\033[33m"
blue="\033[34m"
purple="\033[35m"
cyan="\033[36m"
white="\033[37m"
background="\033[0m"
token="\u0037\u0037\u0038\u0037\u0030\u0036\u0034\u0034\u0066\u0061\u0032\u0033\u0063\u0031"
function ErrorReturn(){
echo -en "
${white}————————————————————
    脚本运行出现${red}错误$white,请及时${blue}截图${white},保存报错信息,
    以与便开发者,${green}更好的${white}和${green}更快的${cyan}解决${white}问题.
${white}————————————————————${background}"
exit 1
}
function SelectRegion(){
Tips(){
echo && echo -en "
 ———————————————————
 ${cyan}请问您的设备所在位置是? 
    ${green}1${white}. 中国大陆 ${background}
    ${green}2${white}. 其他位置 ${background}
 ———————————————————
 ${cyan}请输入数字 ${white}[1-2]:${background} "
}
read -p "$(Tips)" num
case ${num} in
1)
return 0
;;
2)
return 1
;;
*)
echo -e ${blue}[${red}*${blue}] ${cyan}输入错误 ${yellow}保持默认 ${purple}中国大陆.${background}
return 0
esac
}
function CheckNetwork(){
IpinfoIo=$(curl -sL ipinfo.io/json)
if [ -z "${IpinfoIo}" ]
then
  if SelectRegion
  then
    return 0
  else
    return 1
  fi
else
  if echo ${IpinfoIo} | grep -q error
  then
    token=$(echo -e ${token})
    if curl -sL ipinfo.io/json?token=${token} | jq .country | grep -q "CN"
    then
      return 0
    else
      return 1
    fi
  else
    if echo ${IpinfoIo} | jq .country | grep -q "CN"
    then
      return 0
    else
      return 1
    fi
  fi
fi
}
function IncreCounter(){
  if [ -z ${count} ]
  then
    count=0
  fi
  ((count++))
  case ${count} in
    3)
      count=0
      return 1
  esac
  return 0
}
function Download(){
file="$1"
URL="$2"
local i=0
DownloadStart(){
  echo -e ${blue}[${green}*${blue}] ${cyan}正在下载 ${yellow}${file}${cyan}${background}
  until ${Command}
  do
    if ! IncreCounter
    then
      echo -e ${blue}[${red}*${blue}] ${cyan}错误次数过多 ${yellow}退出${background}
      return 1
    fi
    echo -e ${blue}[${red}*${blue}] ${cyan}下载失败 ${yellow}三秒后重试${background}
    sleep 3s
  done
  echo -e ${blue}[${green}*${blue}] ${cyan}下载完成.${background}
  return 0
}
if $(command -v wget > /dev/null 2>&1) && $(wget --help all | grep -q show-progress)
then
  Command="wget --quiet --show-progress --output-document="${file}" --continue "${URL}""
  DownloadStart
elif $(command -v curl > /dev/null 2>&1) && $(curl --help all | grep -q progress-bar)
then
  Command="curl --output "${file}" --progress-bar --location --continue-at - "${URL}""
  DownloadStart
elif $(command -v wget > /dev/null 2>&1)
then
  Command="wget --output-document="${file}" --continue "${URL}""
  DownloadStart
else
  echo -e ${blue}[${red}*${blue}] ${cyan}无下载器.${background}
  return 1
fi
}

function ChromiumInstall(){
if ! dpkg -s gnupg > /dev/null 2>&1
then
  apt install -y gnupg
fi
if CheckNetwork
then
  SourcesListFile="/etc/apt/sources.list.d/xtradeb-ubuntu-apps-jammy.list"
  SourcesList="deb https://launchpad.proxy.ustclug.org/xtradeb/apps/ubuntu/ $1 main"
  GpgFilePath="/etc/apt/trusted.gpg.d/xtradeb-ubuntu-apps.gpg"
  echo ${SourcesList} > ${SourcesListFile}
  KeyURLList=("keyserver.arcticfox.top" "keyserver.ubuntu.com" "pgp.mit.edu")
  for KeyURL in ${KeyURLList}
  do
    rm ${GpgFilePath} > /dev/nll 2>&1
    GpgFileURL="https://${KeyURL}/pks/lookup?op=get&search=0x5301fa4fd93244fbc6f6149982bb6851c64f6880"
    if Download xtradeb-ubuntu-apps.gpg ${GpgFileURL}
    then
      break
    fi
  done
  gpg --dearmor -o ${GpgFilePath} xtradeb-ubuntu-apps.gpg
  apt -y update
  apt install -y chromium
  rm -rf xtradeb-ubuntu-apps.gpg
else
  if ! dpkg -s software-properties-common > /dev/null 2>&1
  then
    apt install -y software-properties-common
  fi
  until echo -e '\n' | add-apt-repository ppa:xtradeb/apps
  do
    echo -e ${red}添加PPA仓库失败 3秒后重试${background}
    sleep 3s
  done
  apt -y update
  apt install -y chromium
fi
}
if grep -q -i focal /etc/os-release
then
  if ! bash <(curl https://gitee.com/baihu433/chromium/raw/master/chromium.sh)
  then
    ErrorReturn
  fi
elif grep -q -i jammy /etc/os-release
then
  if ! ChromiumInstall jammy
  then
    ErrorReturn
  fi
elif grep -q -i noble /etc/os-release
then
  if ! ChromiumInstall noble
  then
    ErrorReturn
  fi
elif grep -q -i oracular /etc/os-release
then
  if ! ChromiumInstall oracular
  then
    ErrorReturn
  fi
else
  apt install -y chromium-browser || apt install -y chromium
fi
